﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongCat;

namespace QuanLyKho.QuanLyKho
{
    public partial class DanhSachLenhPopup : Form
    {
        public DanhSachLenhPopup()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
        }

        public void Loaddata()
        {
            gridLenh.DataSource = DataAll.SelectAll("LenhSanXuat inner join PhongMau_SanPham On LenhSanXuat.IdSanPham = PhongMau_SanPham.IdSanPham",
                "LenhSanXuat.[ID],PhongMau_SanPham.[MaHang] as MaSanPham,LenhSanXuat.[IdSanPham],[NgayLapLenh],[NguoiLapLenh],LenhSanXuat.[SoLuong],[NgayGuiLenh],[NgaySuaLenh],[NgayDuyetLenh],LenhSanXuat.[TrangThai],[NgayChungTu],[NgayHoachToan],LenhSanXuat.[SoChungTu],LenhSanXuat.[GhiChu],[KhoDen],[KhoDi],[SXTuNgay],[SXDenNgay]",
                "WHERE LenhSanXuat.TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_ChotLenh));
        }

        public void SetPropertiesGrid()
        {
            gvLenh.BestFitColumns();
            gvLenh.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvLenh.OptionsSelection.MultiSelect = true;
            gvLenh.OptionsView.ShowGroupPanel = false;
            gvLenh.OptionsView.ShowFooter = true;
            gvLenh.OptionsFind.AlwaysVisible = true;
            gvLenh.Columns["ID"].Visible = false;
            for (int i = 3; i < gvLenh.Columns.Count; i++)
            {
                gvLenh.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        private void gvLenh_DoubleClick(object sender, EventArgs e)
        {
            CapNhatLenh.IdLenh = gvLenh.Columns["ID"].View.GetRowCellDisplayText(gvLenh.FocusedRowHandle, "ID");
            var lss = new CapNhatLenh();
            lss.ShowDialog();
            Loaddata();
            CapNhatLenh.IdLenh = "0";
        }

        private void btnCapNhat_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvLenh.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn cập nhật kho.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("LenhSanXuat", "TrangThai =" + Convert.ToInt32(TrangThai.LenhSanXuat_CapNhatKho) + " WHERE ID =" + dataview[gvLenh.FocusedRowHandle]["ID"]);
                if (message.Equals("success"))
                {
                    try
                    {
                        var data = DataAll.SelectAll("LenhSanXuat_ChiTiet", "*", " WHERE IDLenhSanXuat = " + gvLenh.GetDataRow(gvLenh.FocusedRowHandle)["ID"]);
                        for (int i = 0; i < data.Rows.Count; i++)
                        {
                            var soluong = DataAll.SelectAll("Kho_VatTu", "SoLuong", " WHERE MaVatTu=N'" + data.Rows[i]["MaVatTu"] + "'");
                            DataAll.Update("Kho_VatTu", "SoLuong = " + Convert.ToString(Convert.ToInt32(soluong.Rows[0]["SoLuong"]) - Convert.ToInt32(data.Rows[i]["SoLuongCan"])) + " WHERE MaVatTu=N'" + data.Rows[i]["MaVatTu"] + "'");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Sửa số lượng vật tư dự kiến \n" + ex, "Message", MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }

                    DataAll.InsertAll("HanhTrinh", dataview[gvLenh.FocusedRowHandle]["MaSanPham"]
                        + "," + Convert.ToInt32(PhongBan.PhongLenh)
                        + "," + dataview[gvLenh.FocusedRowHandle]["SoChungTu"]
                        + "," + Convert.ToInt32(TrangThai.LenhSanXuat_CapNhatKho)
                        + "," + dataview[gvLenh.FocusedRowHandle]["NguoiLapLenh"]
                        + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                        + "," + dataview[gvLenh.FocusedRowHandle]["SoLuong"]
                        + "," + Convert.ToDateTime(dataview[gvLenh.FocusedRowHandle]["TuNgay"]).ToString("MM/dd/yyyy")
                        + "," + Convert.ToDateTime(dataview[gvLenh.FocusedRowHandle]["DenNgay"]).ToString("MM/dd/yyyy"));
                    
                }
                MessageBox.Show(message.Equals("success") ? "Cập nhật kho thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Loaddata();
            }
        }

        private void btnKhongCapNhat_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvLenh.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn từ chối cập nhật vào kho.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("LenhSanXuat", "TrangThai =" + Convert.ToInt32(TrangThai.LenhSanXuat_ChoDuyet) + " WHERE ID =" + dataview[gvLenh.FocusedRowHandle]["ID"]);
                MessageBox.Show(message.Equals("success") ? "Từ chối cập nhật thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Loaddata();
            }
        }

        private void gvLenh_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }
    }
}
